package com.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.dao.DoctorDao;
import com.hospital.entity.Doctor;
import com.hospital.service.DoctorService;
import com.hospital.vo.DoctorVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zt
 * @Date 2022/3/28
 */
@Slf4j
@DubboService
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorDao doctorDao;

    @Override
    public List<Doctor> getAllDoctor(int pageNum, int pageSize) {
        List<Doctor> doctorEntities = doctorDao.findAllDoctor(pageNum, pageSize);
        List datalist = new ArrayList();
        doctorEntities.forEach(doctor -> {
            BeanUtils.copyProperties(Doctor.class, new DoctorVO());
            datalist.add(doctor);
        });
        return datalist;
    }

    @Override
    public void addDoctor(DoctorVO doctorVO) {
        try {
            Doctor doctor = BeanUtil.copyProperties(doctorVO, Doctor.class);
            doctorDao.addDoctor(doctor);
        } catch (RuntimeException e) {
            log.error(e.getMessage());
            new Throwable("添加医生失败", e);
        }
    }

    @Override
    public DoctorVO getByNameAndDeptId(String doctorName, Integer deptId) {
        Doctor doctor = doctorDao.findByNameAndDept(doctorName, deptId);
        DoctorVO doctorVO = BeanUtil.copyProperties(doctor, DoctorVO.class);
        return doctorVO;
    }

    @Override
    public DoctorVO doctorInfo(Integer id) {
        Doctor doctor = doctorDao.doctorInfo(id);
        DoctorVO doctorVO = BeanUtil.copyProperties(doctor, DoctorVO.class);
        return doctorVO;
    }

    @Override
    public void updateDoctor(DoctorVO doctorVO) {
        try {
            Doctor doctor = BeanUtil.copyProperties(doctorVO, Doctor.class);
            doctorDao.updateDoctor(doctor);
        } catch (RuntimeException e) {
            log.error(e.getMessage());
            new Throwable("用户更新失败", e);
        }
    }

    @Override
    public void delDoctor(Integer id) {
        try {
            doctorDao.deleteDoctor(id);
        } catch (RuntimeException e) {
            log.error(e.getMessage());
            new Throwable("删除用户失败", e);
        }
    }

}
